/**
 * 
 */
package com.funo.wcity.app.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.funo.wcity.app.entity.LoginUser;
import com.funo.wcity.app.entity.TblUserMail;
import com.funo.wcity.app.entity.TblUsers;
import com.funo.wcity.app.service.TblUserMailService;
import com.funo.wcity.app.service.TblUsersService;
import com.funo.wcity.app.utils.CacheUtil;
import com.funo.wcity.app.utils.Constant;
import com.funo.wcity.app.utils.Base64Util;
import com.funo.wcity.app.utils.StringUtil;

/**
 * @author Administrator
 *
 */
@Controller
public class TblUserMailController extends CommonController {
	@Resource(name="tblUserMailService")
	private TblUserMailService tblUserMailService;
	@Resource(name="tblUsersService")
	private TblUsersService tblUsersService;
	/**
	 * 设置帐号
	 */
	@RequestMapping("/setUserAcc")
	public String setUserAcc(HttpServletRequest request,HttpServletResponse response,Integer idUserMail){
		LoginUser loginUser=(LoginUser)request.getSession().getAttribute(Constant.LOGIN_USER);
		TblUsers tblUsers=this.tblUsersService.getTblUsers(loginUser.getIdUser());
		TblUserMail tblUserMail=this.tblUserMailService.getTblUserMail(idUserMail);
		request.setAttribute("tblUsers",tblUsers);
		request.setAttribute("tblUserMail",tblUserMail);
		request.setAttribute("idUserMail", idUserMail);
		//所有邮箱列表
		request.setAttribute("tblUserMailList",this.tblUserMailService.getTblUserMailList(loginUser.getIdUser()));
	    request.setAttribute("defEmail",CacheUtil.defMail);
		return "web/set_mail";
	}
	
	/***
	 * 修改设置信息
	 */
	@RequestMapping("/saveInfo")
	public String saveInfo(HttpServletRequest request,HttpServletResponse response,TblUsers tblUsers,TblUserMail tblUserMail){
		//保存用户信息
		TblUsers users=this.tblUsersService.getTblUsers(tblUsers.getIdUser());
		users.setRemark(StringUtil.reTrimByString(tblUsers.getRemark()));
		this.tblUsersService.saveTblUsers(users);
		//保存邮箱
		TblUserMail userMail=this.tblUserMailService.getTblUserMail(tblUserMail.getIdUserMail());
		userMail.setPop(tblUserMail.getPop());
		userMail.setSmtp(tblUserMail.getSmtp());
		userMail.setIsidentity(tblUserMail.getIsidentity());
		userMail.setIstiming(tblUserMail.getIstiming());
		userMail.setIntervaltime(tblUserMail.getIntervaltime());
		userMail.setImap(tblUserMail.getImap());
		this.tblUserMailService.saveTblUserMail(userMail);
		//将默认帐号保存到缓存中，以最新的缓存为主
		CacheUtil.defMail=request.getParameter("defEmail");
		return "web/opera_success";
	}
	/**
	 * 查询当前用户帐号列表
	 */
	@RequestMapping("/listAccount")
	public String listAccount(HttpServletRequest request,HttpServletResponse response,Integer idUserMail){
		LoginUser loginUser=(LoginUser)request.getSession().getAttribute(Constant.LOGIN_USER);
		List userMailList=this.tblUserMailService.getTblUserMailList(loginUser.getIdUser());
		request.setAttribute("userMailList", userMailList);
		request.setAttribute("idUserMail", idUserMail);
		return "web/account_list";
	}
	
	/**
	 * 设置修改或新增帐号_界面
	 */
	@RequestMapping("/setAccount")
	public String setAccount(HttpServletRequest request,HttpServletResponse response,Integer idUserMail){
		//帐号ID
		String newIdUserMail=request.getParameter("newIdUserMail");
		//fun:1新增；2修改
		String fun=request.getParameter("fun");
		if(!StringUtil.isEmpty(newIdUserMail)){
			request.setAttribute("tblUserMail",this.tblUserMailService.getTblUserMail(Integer.valueOf(newIdUserMail)));
		}
		request.setAttribute("newIdUserMail",newIdUserMail);
		request.setAttribute("fun",fun);
		request.setAttribute("idUserMail", idUserMail);
		return "web/set_account";
	}
	/**
	 * 保存帐号
	 */
	@RequestMapping("/saveAccount")
	public String saveAccount(HttpServletRequest request,HttpServletResponse response,TblUserMail tblUserMail){
		String newIdUserMail=request.getParameter("newIdUserMail");
		Integer idUserMail=tblUserMail.getIdUserMail();
		if(!StringUtil.isEmpty(newIdUserMail)){
			tblUserMail.setIdUserMail(Integer.valueOf(newIdUserMail));
		}else{
			tblUserMail.setLastDate(new Date());
			tblUserMail.setIdUserMail(null);
		}
		LoginUser loginUser=(LoginUser)request.getSession().getAttribute(Constant.LOGIN_USER);
		tblUserMail.setIdUser(loginUser.getIdUser());
		Base64Util desUtil=new Base64Util();
		tblUserMail.setPassword(new String(desUtil.encode(tblUserMail.getPassword().getBytes())));
		//保存
		this.tblUserMailService.saveTblUserMail(tblUserMail);
		//查询
		List userMailList=this.tblUserMailService.getTblUserMailList(loginUser.getIdUser());
		request.setAttribute("userMailList", userMailList);
		request.setAttribute("idUserMail",idUserMail);
		return "web/account_list";
	}
	/**
	 * 删除帐号
	 */
	@RequestMapping("/delAccount")
	public String delAccount(HttpServletRequest request,HttpServletResponse response,Integer idUserMail){
		Integer newIdUserMail=Integer.valueOf(request.getParameter("newIdUserMail"));
		//删除
		TblUserMail tblUserMail=this.tblUserMailService.getTblUserMail(newIdUserMail);
		this.tblUserMailService.deleteTblUserMail(tblUserMail);
		//查询
		LoginUser loginUser=(LoginUser)request.getSession().getAttribute(Constant.LOGIN_USER);
		List userMailList=this.tblUserMailService.getTblUserMailList(loginUser.getIdUser());
		request.setAttribute("userMailList", userMailList);
		request.setAttribute("idUserMail",idUserMail);
		return "web/account_list";	
	}
}
